// pages/orderList/orderList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBars: [{
      name: '全部',
      id: '0'
    }, {
      name: '待付款',
      id: '1'
    }, {
      name: '待发货',
      id: '2'
    }, {
      name: '待收货',
      id: '3'
    }, {
      name: '待评价',
      id: '4'
    }],
    list: '',
    status: 0,
    notOnload: false,
  },

  delete(e) {
    let ordersId = e.currentTarget.dataset.id
    this.confirm.hideShow({
      title: '删除订单',
      info: `确认删除订单吗？`,
      args: {
        ope: 'delete',
        ordersId: ordersId
      }
    })
  },

  takeGoods(e) {
    let ordersId = e.currentTarget.dataset.id
    this.confirm.hideShow({
      title: '确认收货',
      info: `确认收货吗？`,
      args: {
        ope: 'receive',
        ordersId: ordersId
      }
    })
  },

  cancel(e) {
    let ordersId = e.currentTarget.dataset.id
    this.confirm.hideShow({
      title: '取消订单',
      info: `确认取消订单吗`,
      args: {
        ope: 'cancel',
        ordersId: ordersId
      }
    })
  },

  pay(e) {
    let orderId = e.currentTarget.dataset.id
    wx.$unifiedorder(orderId)
  },

  ope(e) {
    if (e.detail.type == 1) {
      let args = e.detail.args
      if (args.ope == 'cancel') {
        wx.$https('orders.cancel', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '取消成功',
          })
          setTimeout(() => {
            this.getList(0)
          }, 500)
        })
      } else if (args.ope == 'receive') {
        wx.$https('orders.confirm.receive', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '收货成功',
          })
          setTimeout(() => {
            this.getList(0)
          }, 500)
        })
      } else if (args.ope == 'delete') {
        wx.$https('orders.del', {
          ordersId: args.ordersId
        }).then(res => {
          wx.showToast({
            title: '删除成功',
          })
          setTimeout(() => {
            this.getList(0)
          }, 500)
        })
      }
    }
  },

  getList(start) {
    wx.$https('orders.list', {
      start: start,
      limit: 10,
      status: this.data.status
    }).then(res => {
      res.map(v => {
        v.amount = v.ordersItemsList.reduce((sum, current) => {
          return sum + current.amount
        }, 0)
        return v
      })
      this.setData({
        list: start == 0 ? res : this.data.list.concat(res)
      })
    })
  },

  navEva(e) {
    wx.$nav('/pages/evaluate/evaluate', {
      ordersId: e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status
    })
    if(options.status=='6,7,99'){
      wx.setNavigationBarTitle({
        title: '售后/退款',
      })
    }
    this.getList(0)
    this.setData({
      notOnload: true
    })
  },

  navChange(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      status: id
    })
    this.getList(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.confirm = this.selectComponent('#confirm')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.notOnload)
      this.getList(0)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList(this.data.list.length)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})